package cn.e3mall.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.e3mall.common.pojo.EasyUIDataGridResult;
import cn.e3mall.common.utils.E3Result;
import cn.e3mall.content.service.ContentService;
import cn.e3mall.pojo.TbContent;

@Controller
public class ContentController {

	@Autowired
	private ContentService contentService;
	
	/**
	 * 	根据内容分类Id查询内容列表
	 * @param categoryId
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/content/query/list")
	@ResponseBody
	public EasyUIDataGridResult getContentListById(Long categoryId,
			@RequestParam(defaultValue = "1")Integer page,Integer rows) {
		//若id为空
		if(categoryId == null) {
			return null;
		}
		
		EasyUIDataGridResult result = contentService.getContentListById(categoryId, page, rows);
		
		return result;
	}

	/**
	 * 	添加内容
	 * @param categoryId
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping(value="/content/save", method=RequestMethod.POST)
	@ResponseBody
	public E3Result addContent(TbContent content) {
		
		//若content和 categoryId为空
		if(content == null || content.getCategoryId() == null) {
			return E3Result.build(500, "内容不能全为空");
		}
		
		//调用服务把内容数据保存到数据库
		E3Result e3Result = contentService.addContent(content);
		return e3Result;
	}
	
	/**
	 * 	编辑内容
	 * @param categoryId
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/rest/content/edit")
	@ResponseBody
	public E3Result editContent(TbContent content) {
		
		//若content和 categoryId为空
		if(content == null || content.getCategoryId() == null) {
			return E3Result.build(500, "内容不能全为空");
		}
		
		//调用服务把内容数据保存到数据库
		E3Result e3Result = contentService.editContent(content);
		
		return e3Result;
	}
	
	/**
	 * 	删除内容
	 * @param categoryId
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping(value="/content/delete", method=RequestMethod.POST)
	@ResponseBody
	public E3Result deleteContent(Long[] ids) {
		
		//若删除内容列表为空
		if( ids == null || ids.length == 0) {
			return E3Result.build(500, "没有选择要删除的内容");
		}
		
		//调用服务把内容数据保存到数据库
		E3Result e3Result = contentService.deleteContent(ids);
		
		return e3Result;
	}
}
